var tokenconfig=require('./../../../config/tokenconfig');
var UserModel = require("./../../../models").User;
var verifyToken = function (req, res, next) {
	var token = getToken(req.headers);
	UserModel.findOne({token:token}, function (err, user) {
        if (err)
            return res.json({err:err});
        if(!user){
        	delete req.user;
        }
        next();
    });
};
var getToken = function(headers) {
	if (headers && headers.authorization) {
		var authorization = headers.authorization;
		var part = authorization.split(' ');

		if (part.length == 2) {
			var token = part[1];

			return part[1];
		}
		else {
			return null;
		}
	}
	else {
		return null;
	}
};
module.exports={
	conf:tokenconfig,
	verify:verifyToken
};